﻿using Microsoft.VisualBasic;

public class GenericList<T> {
    private Node<T>? head;
    private Node<T>? tail;

    public GenericList() {
        head = tail = null;
    }

    public Node<T>? Head {
        get => head;
    }
    public void Add(T t) {
        Node<T> n = new Node<T>(t);
        if (tail == null) {
            head = tail = n;
        }
        else {
            tail.Next = n;
            tail = n;
        }
    }

    public void ForEach(Action<Node<T>> action) {
        var tmp = head;
        while (tmp != null) {
            action(tmp);
            tmp = tmp.Next;
        }
    }

    public void GetMaxOrMin(Func<Node<T>, Node<T>, bool> compare) {
        Node<T> ret = head;
        Node<T> tmp = head;
        while (tmp != null) {
            ret = compare(ret, tmp) ? ret : tmp;
            tmp = tmp.Next;
        }
        Console.WriteLine(ret.Data);
    }

    public double Sum(Func<Node<T>, double> func) {
        double sum = 0;
        var tmp = head;
        while (tmp != null) {
            sum += func(tmp);
            tmp = tmp.Next;
        }
        return sum;
    }
}